/*
 * @(#)ApplicationException.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import org.apache.struts.action.ActionError;

/**
 * ApplicationException.java
 *
 * This class is to keep the application error.
 *
 * @author          Rollo Chan
 * @company         DCIVision Limited
 * @creation date   24/06/2003
 * @version         $Revision: 1.5.32.3 $
 */
public class ApplicationException extends ServletException {

  public static final String REVISION = "$Revision: 1.5.32.3 $";

  private String msgCode;
  private String[] msgArg;
  private Exception srcException;

  /**
   * Constructor
   */
  public ApplicationException() {
    super();
  }

  /**
   * Constructor
   *
   * @param msgCode
   */
  public ApplicationException(String msgCode) {
    super(MessageResourcesFactory.getMessage(null, msgCode));
    this.msgCode = msgCode;     
  }

  /**
   * Constructor
   *
   * @param srcException
   */
  public ApplicationException(Exception srcException) {
    super(srcException);
    this.srcException = srcException;
  }

  /**
   * Constructor
   *
   * @param msgCode
   * @param srcException
   */
  public ApplicationException(String msgCode, Exception srcException) {
    super(MessageResourcesFactory.getMessage(null, msgCode), srcException);
    this.msgCode = msgCode;
    this.srcException = srcException;
  }

  /**
   * Constructor
   *
   * @param msgCode
   * @param srcException
   * @param msgArgStr
   */
  public ApplicationException(String msgCode, Exception srcException, String msgArgStr) {
    super(MessageResourcesFactory.getMessage(null, msgCode), srcException);
    this.msgCode = msgCode;
    this.srcException = srcException;
    this.msgArg = new String[]{msgArgStr};
  }

  /**
   * Constructor
   *
   * @param msgCode
   * @param srcException
   * @param msgArg
   */
  public ApplicationException(String msgCode, Exception srcException, String[] msgArg) {
    super(MessageResourcesFactory.getMessage(null, msgCode), srcException);
    this.msgCode = msgCode;
    this.srcException = srcException;
    this.msgArg = msgArg;
  }

  /**
   * getMsgCode
   *
   * @return the error message code
   */
  public String getMsgCode() {
    return(this.msgCode);
  }

  /**
   * getSrcException
   *
   * @return the source exception object
   */
  public Exception getSrcException() {
    return(this.srcException);
  }

  /**
   * getActionError
   *
   * @return the action error after manipulation
   */
  public ActionError getActionError() {
    return(new ActionError(this.msgCode, msgArg));
  }

  public void printStackTrace(PrintWriter s) {
    super.printStackTrace(s);
    if( srcException !=null) {
      s.print("Caused By :");
      srcException.printStackTrace(s);
    }
  }
}
